﻿<?php
/*
 Plugin Name: Cool Diary
 Plugin URI: http://blog.shareworth.net/cooldiary
 Description: Make your wordpress as a diary.You can manager who can vistor and how to display your website.
 Author: ywzlcm@gmail.com
 Version: 1.0.2
 Author URI: http://blog.shareworth.net
 */

require_once  'php/function.php';
/**
 * Check whether the object has been initlizated.
 */
if (!class_exists("CoolDiaryPlugin")){
	class CoolDiaryPlugin
	{
		/* All Plugin functions...... */

		function coolDiary_initMyPlugin ()
		{
			require_once  'php/function.php';

			//wp_enqueue_script('diary-post-js');
			$plugin_dir = basename(dirname(__FILE__)).'/languages';
			load_plugin_textdomain( 'CoolDiary',false, $plugin_dir);


		}

		function coolDiary_showWeather() {

			//	$data = get_post_meta($post->ID, 'coolDiary');



			$data = get_post_meta(290,'coolDiary',false);
			return '<div id="weather">天气'.$data["city"].' '.$data["weather"].' '.$data["mood"].'</div>';
			/*
			 //心情：<img src="http://yourwebsite/mood/<?php echo get_post_meta($post->ID, "mood", $single = true); ?>.png" alt="<?php echo get_post_meta($post->ID, "mood", true); ?>" /> 天气：<img src="http://yourwebsite/weather/<?php echo get_post_meta($post->ID, "weather", $single = true); ?>.png" alt="<?php echo get_post_meta($post->ID, "weather", true); ?>" />
			 */
			//return '<div id="twitit"><a href="http://twitter.com/home?status=Currently reading '.get_permalink($post->ID).'" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a></div>';

		}


		/**
		 * Add a metabox to the post page
		 * @param unknown_type $page
		 * @param unknown_type $context
		 * @param unknown_type $object
		 */
		function coolDiary_registerMetaBoxes( $page, $context, $object)
		{
			if (in_array($page, array('page', 'post'))) {
				global $wp_meta_boxes;
				unset($wp_meta_boxes['post']['normal']['core']['postcustom']);


				$callback = 'coolDiary_render' . ucfirst($page) . 'MetaBox';

				add_meta_box(
				attribute_escape('new-meta-boxes'), // id attribute
				wp_specialchars(__("Mood & Weather", 'CoolDiary')),   // metabox title
				array(&$this, $callback) ,          // callback function
				$page,                              // page type
				'side', 							//
				'high'
				);

				add_action('admin_print_scripts-post-new.php', array(&$this,'coolDiary_Styles'));

			}



		}

		function coolDiary_Styles()
		{
			/*
			 * It will be called only on your plugin admin page, enqueue our script here
			 */
			wp_register_script('diary-post-js', WP_PLUGIN_URL . '/CoolDiary/js/diary-post.js.php',false, '1.4', true);
			wp_enqueue_script('diary-post-js');
		}

		/**
		 * Cool Diary post metaBox page
		 */
		function coolDiary_renderPostMetaBox(){

			require_once 'php/diary-post.php';
		}

		/**
		 * Add the opinion to the admin menu
		 */
		function coolDiary_addOpinionPageMenu(){
			$page = add_submenu_page('post-new.php', __("Diary Setting", 'CoolDiary'), //$page_title
			__("Diary Setting", 'CoolDiary'), //$menu_title
			'administrator', //$capability
			basename(__FILE__), //$menu_slug
			array(&$this,'coolDiary_renderOptionPage')//callback function
			);
			/* Using registered $page handle to hook script load */


		}

		/**
		 * Cool Diary Setting page
		 */
		function coolDiary_renderOptionPage(){
			require_once  'php/diary-option.php';
		}

		function coolDiary_safePost($post_id){

			$weather = $_POST[COOL_DIAEY_PRIFIX.'weather'];
			$mood = $_POST[COOL_DIAEY_PRIFIX.'mood'];
			$city = $_POST[COOL_DIAEY_PRIFIX.'city'];

			update_post_meta($post_id, 'coolDiary', array('weather'=>$weather,'mood'=>$mood,'city'=>$city));
			//			update_post_meta($post_id, 'coolDiary_mood', 'happy');
		}

	}
}


/**
 * Initiation ...
 */
if (class_exists("CoolDiaryPlugin")){
	$coolDiaryPlugin = new CoolDiaryPlugin();
}
/**
 * Filter the action
 */
if (isset($coolDiaryPlugin)){

	add_action('init',array(&$coolDiaryPlugin, 'coolDiary_initMyPlugin'));


	add_shortcode('showWeather', array(&$coolDiaryPlugin, 'coolDiary_showWeather'));

	add_action('admin_menu', array(&$coolDiaryPlugin, 'coolDiary_addOpinionPageMenu'));//admin menu

	add_action('do_meta_boxes',array(&$coolDiaryPlugin,'coolDiary_registerMetaBoxes'),8,3);//post matebox

	add_action('save_post',array(&$coolDiaryPlugin,'coolDiary_safePost'));//post matebox

	//add_action('activate_....php', array(&$MyPlugin, ...));
	//add_action('admin_menu', array(&$MyPlugin, ...));
	//add_filter('the_content', array(&$MyPlugin, ...));

	add_action( 'wp_ajax_getWeather', 'getWeatherSimple' );



}






/* EOF */
